

var util = {};

util.confirm = function (obj) {

	if(typeof obj === 'string') obj = {text: obj};

	var defaultOpt = {
		title: '',
		text: '',
		type: 'confirm',
		confirm: function() {},
		cancel: function() {}
	};

	var opt = $.extend({}, defaultOpt, obj);

	var modal = $('.alert-modal-wrap'), // 模态框
		confirmBtn = null, // 模态框确认按钮
		closeBtn = null, // 关闭按钮
		cancelBtn = null;  // 模态框取消按钮

	createModal();

	/**
	 * []
	 * @return {[type]} [description]
	 */
	function createModal() {
		var elem = null,
			titleClass = opt.title ? '' : 'hide';

		if (modal.length <= 0) {
			elem = document.createElement('div');
			elem.className = 'alert-modal-wrap';
			elem.innerHTML = '<div class="modal-musk"></div><div class="modal-content"><span class="close-modal"><i class="iconfont">×</i></span><h1 class="center-title '+ titleClass +'">'+ opt.title +'</h1><p class="msg">'+ opt.text +'</p><div class="btn-wrap"><button class="btn x-btn-yellow confirm">确定</button><button class="btn x-btn cancel">取消</button></div></div>';
			modal = $(elem);

			document.body.appendChild(elem);
		} else {
			modal.removeClass('hide').find('.msg').text(opt.text);
		}

		confirmBtn = modal.find('.confirm');
		cancelBtn = modal.find('.cancel');
		closeBtn = modal.find('.close-modal');

		bindEvent();
	};

	/**
	 * 绑定确认， 取消， 键盘事件
	 * @return {[type]} [description]
	 */
	function bindEvent() { 

		// 确认回调
	 	function confirmCb() {
	 		opt.confirm();
	 		modal.addClass('hide');
	 		$(document).unbind('keyup', keyboardEvent);
	 	}

	 	// 取消回调
	 	function cancelCb() {
	 		opt.cancel();
			modal.addClass('hide');
			$(document).unbind('keyup', keyboardEvent);
	 	}

	 	function keyboardEvent(e) {
			// 回车事件
			if(event.keyCode == 13) {
		  		confirmCb();
		  	} 

		  	// esc事件
		  	if (event.keyCode == 27) {
		  		cancelCb();
		  	}
		} 

		confirmBtn.unbind('click').one('click', function() {
			confirmCb();
		}); 

		closeBtn.unbind('click').one('click', function() {
			cancelCb();
		}); 

		cancelBtn.unbind('click').one('click', function() {
			cancelCb();
		}); 

		$(document).unbind('keyup').bind('keyup', keyboardEvent);
	}
};

util.alert = function(str, type, fn) {
	var text = str || '保存成功！',
		successClass = 'show',
		errorClass = '',
		delClass = '';

	if (type == 'error') {
		successClass = '';
		delClass = '';
		errorClass = 'show';

		if (!str) {
			text = '操作失败';
		}
	}

	if (type == 'delete') {
		successClass = '';
		delClass = 'show';
		errorClass = '';
	}

	var modal = $('#alert-success-modal'),
		timer = null;
	createModal();

	timer = setTimeout(function() {
 		modal.addClass('hide');

 		if (fn) fn();
 	}, 2000);
	
	function createModal() {
		var elem = null;

		if (modal.length <= 0) {
			elem = document.createElement('div');
			elem.className = 'x-modal-wrap';
			elem.id = 'alert-success-modal';
			elem.innerHTML = '<div class="content">'+
					 			'<i class="icon success iconfont '+ successClass +'">&#xe632;</i><i class="icon error iconfont '+ errorClass +'">&#xe641;</i><i class="icon delete iconfont '+ delClass +'">&#xe63e;</i>'+
					 			'<p style="font-size: 18px;" class="msg">'+ text +'</p>'+
					 		'</div>'; 

			document.body.appendChild(elem);
			modal = $(elem);
		} else {
			modal.removeClass('hide');
			modal.find('.icon').removeClass('show');
			modal.find('.success').addClass(successClass);
			modal.find('.delete').addClass(delClass);
			modal.find('.error').addClass(errorClass);
			modal.find('.msg').text(text);
		}
 	}
}

/**
 * ajax 提交表单
 * @param  {[DOM]}   form     [要提交的表单]
 * @param  {Function} callback [ajax成功之后的回调]
 * @return {[type]}            [description]
 */
util.submitForm = function(form, callback) {

	var options = {  
	   success: success,      //提交后的回调函数  
	   error: error, 
	   dataType: 'json',           //html(默认), xml, script, json...接受服务端返回的类型  
	   clearForm: false,          //成功提交后，清除所有表单元素的值  
	   resetForm: false,          //成功提交后，重置所有表单元素的值  
	   timeout: 10000               //限制请求的时间，当请求大于10秒后，跳出请求  
	};

	function success(json) {  
		if (json.code == 1) {
			util.alert('', 'success');

			// 如果callback存在， 页面不跳转， 如需跳转， 在callback里面跳转
			if (callback) {
				callback(json);
			} else {
				setTimeout(function() {
					window.location.href = json.url;
				}, 1000);
			}
		} else {
			util.alert(json.msg, 'error');
		}
	}

	function error() {  
		util.alert('保存失败', 'error');
	}

	$(form).ajaxSubmit(options);
}


util.formModal = function (obj) { 
	obj = obj || {};

	var defaultOpt = {
		id: '#form-modal-wrap',
		html: '空', 
		show: true,
		confirm: function() {},
		cancel: function() {}
	};

	var opt = $.extend({}, defaultOpt, obj);

	var modal = $(opt.id), // 模态框
		confirmBtn = null, // 模态框确认按钮
		closeBtn = null, // 关闭按钮
		cancelBtn = null;  // 模态框取消按钮

	createModal();

	/**
	 * []
	 * @return {[type]} [description]
	 */
	function createModal() { 
		var elem = null,
			className = opt.show ? 'form-modal-wrap' : 'form-modal-wrap hide';

		if (modal.length <= 0) {
			elem = document.createElement('div');
			elem.className = className;
			elem.id = 'form-modal-wrap';
			elem.innerHTML = ''+ 
							'<div class="modal-musk"></div>'+ 
							'<div class="modal-content">'+
								'<span class="close-modal"><i class="iconfont">×</i></span>'+
								 opt.html +
								// '<div class="btn-wrap">'+
								// 	'<button type="button" class="btn x-btn-yellow confirm">确定</button>'+
								// 	'<button type="button" class="btn x-btn cancel">取消</button>'+
								// '</div>'+
							'</div>';
			modal = $(elem);

			document.body.appendChild(elem);
		} else {
			modal.removeClass('hide');
		}

		confirmBtn = modal.find('.confirm');
		cancelBtn = modal.find('.cancel');
		closeBtn = modal.find('.close-modal');

		bindEvent();
	};

	/**
	 * 绑定确认， 取消， 键盘事件
	 * @return {[type]} [description]
	 */
	function bindEvent() { 

		// 确认回调
	 	function confirmCb() {
	 		opt.confirm();
	 		// modal.addClass('hide');
	 		// $(document).unbind('keyup', keyboardEvent);
	 	}

	 	// 取消回调
	 	function cancelCb() {
	 		opt.cancel();
			modal.addClass('hide');
			$(document).unbind('keyup', keyboardEvent);
	 	}

	 	function keyboardEvent(event) { 
			// 回车事件
			if(event.keyCode == 13) { 
		  		confirmCb();
		  	} 

		  	// esc事件
		  	if (event.keyCode == 27) {
		  		cancelCb();
		  	}
		} 

		confirmBtn.unbind('click').one('click', function() {
			confirmCb();
		}); 

		closeBtn.unbind('click').one('click', function() {
			cancelCb();
		}); 

		cancelBtn.unbind('click').one('click', function() {
			cancelCb();
		}); 

		$(document).unbind('keyup').bind('keyup', keyboardEvent);
	}
}
